package com.burst.reptile.modular.zp.warpper;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.utils.DateUtil;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.config.SpringContextHolder;
import com.burst.reptile.framework.constant.ConstantFactory;
import com.burst.reptile.framework.security.LoginUser;
import com.burst.reptile.framework.security.service.TokenService;
import com.burst.reptile.framework.web.warpper.BaseControllerWrapper;
import com.burst.reptile.modular.api.vo.BaseUser;
import com.burst.reptile.modular.system.entity.SysUser;
import com.burst.reptile.modular.zp.entity.ZpComment;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;

public class ZpWxCommentWrapper extends BaseControllerWrapper<ZpComment> {

    public ZpWxCommentWrapper(Page<ZpComment> page) {
        super(page);
    }
    
    public ZpWxCommentWrapper(List<ZpComment> list) {
        super(list);
    }
    
    @Override
    protected void wrapTheMap(ZpComment comment) {
    	Long userId = comment.getUserId();
    	SysUser user = ConstantFactory.me().getUser(userId);
    	if(ToolUtil.isNotEmpty(user)) {
    		BaseUser baseUser = new BaseUser();
    		BeanUtil.copyProperties(user, baseUser);
    		comment.setUser(baseUser);
    	}
    	String ctype = comment.getCtype();
    	if(StrUtil.equals(ctype, "0")) { // 评论
    		// 回复条数
    		comment.setReplyNum(ConstantFactory.me().replyNum(comment.getCid()));
    	}
    	TokenService tokenService = SpringContextHolder.getBean("tokenService");
    	LoginUser loginUser = tokenService.getWxUser();
    	if(ToolUtil.isNotEmpty(loginUser)) {
    		Long loginUserId = loginUser.getUser().getUserId();
    		// 是否点赞
    		comment.setSupport(ConstantFactory.me().isSupport(loginUserId, comment.getCid()));
    		// 是否是自己评论的
    		comment.setOwn(userId.equals(loginUserId));
    	}
    	comment.setTime(DateUtil.timeToFormat(comment.getCreateTime()));
    	// 评论点赞数
    	comment.setLikes(ConstantFactory.me().getCommentLikes(comment.getCid()));
    }

}
